package com.igoal.dao;

import java.util.Date;
import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.igoal.entity.DataStatus;
import com.igoal.entity.MemberMail;
import com.igoal.utils.CalendarUtils;
import com.igoal.web.Constants;

@Repository("memberMailDAO")
public class MemberMailDAOImpl extends GenericDaoHibernate<MemberMail, Long>
		implements MemberMailDAO {

	public MemberMailDAOImpl() {
		super(MemberMail.class);
	}

	public MemberMail getTopPriorityTask() {
		DetachedCriteria dc = DetachedCriteria.forClass(MemberMail.class);
		dc.addOrder(Order.desc("priority"));
		dc.add(Restrictions.eq("sendingStatus", Constants.SCHEDULED_PENDING));
		dc.add(Restrictions.between("scheduledDate", CalendarUtils.getStartDate(new Date()), CalendarUtils.getEndDate(new Date())));
		getByStatus(dc, DataStatus.USING);
		
		List<MemberMail> result = findByCriteria(dc, 0, 1);
		if(result.size() > 0){
			return result.get(0);
		}
		
		return null;
	}

}
